package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import calculator.auxiliary_I_Team;

import Store.CDElement;
import Store.Enumeration;
import Store.Class;

import Boundary.UpdateInputCD;

public class ShowClassDiagram 
{
	ShowClassDiagram self=this;
	public JPanel container;
	private JTable tabClass; 
	private JTable tabEnum;
	UpdateInputCD updateInputCD;
	
	public String selected = null;
	
	public ShowClassDiagram(UpdateInputCD updateInputCD) 
	{
		this.updateInputCD=updateInputCD;
	}

	public JPanel showClassDiagram()
	{
		container = new JPanel();
		container.setLayout(new BorderLayout());
		container.add(getNorthLabel(),BorderLayout.NORTH);
		container.add(getCentralArea(), BorderLayout.CENTER);
		container.add(getEastButtonArea(), BorderLayout.EAST);
		
		return container;
	}
	
	private JPanel getEastButtonArea() 
	{
		JPanel panel = new JPanel();
		JButton AddClass = new JButton("Add new Class");
		AddClass.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				display(new CD_ManageClass().show(self, false, null, updateInputCD.availableTypes));
				selected=null;
			}
		});
		JButton AddEnum = new JButton("Add new Enumeration");
		AddEnum.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				display(new CD_ManageEnumeration().show(self, false, null));
				selected=null;
			}
		});
		JButton DelElem = new JButton("Delete Element");
		DelElem.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tabClass.getSelectedRow()!=-1)
					updateInputCD.deleteCDElement(updateInputCD.main.OPEN_PRJ.inputCD.getInfo().getClassi().get(tabClass.getSelectedRow()));
				if(tabEnum.getSelectedRow()!=-1)
					updateInputCD.deleteCDElement(updateInputCD.main.OPEN_PRJ.inputCD.getInfo().getEnums().get(tabEnum.getSelectedRow()));
			}
		});
		JButton ModElem = new JButton("Modify Element");
		ModElem.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				CDElement cde=null;
				if(tabClass.getSelectedRow()!=-1)
					cde = updateInputCD.main.OPEN_PRJ.inputCD.getInfo().getClassi().get(tabClass.getSelectedRow());
				if(tabEnum!=null && tabEnum.getSelectedRow()!=-1)
					cde = updateInputCD.main.OPEN_PRJ.inputCD.getInfo().getEnums().get(tabEnum.getSelectedRow());
				if(cde==null)
					JOptionPane.showMessageDialog(container, "There's no Class Selected", "Error", JOptionPane.ERROR_MESSAGE);
				else
				{
					if(cde.getClass()==Enumeration.class)
						display(new CD_ManageEnumeration().show(self, true, (Enumeration)cde));
					else if (cde.getClass()==Class.class)
						display(new CD_ManageClass().show(self, true, (Class)cde, updateInputCD.availableTypes));
				}
				}
		});
		JButton SetContext= new JButton("Set Context Class");
		SetContext.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tabClass.getSelectedRow()!=-1)
					updateInputCD.setContextClass(updateInputCD.main.OPEN_PRJ.inputCD.getInfo().getClassi().get(tabClass.getSelectedRow()));
				else JOptionPane.showMessageDialog(container, "There's no Class Selected", "Error", JOptionPane.ERROR_MESSAGE);
			}
		});
		JButton ValidateElem = new JButton("Validate Element");
		JButton Close = new JButton("Close");
		Close.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				updateInputCD.close();
			}
		});
		
		panel.setLayout(new GridLayout(7,1));
		panel.add(AddClass);
		panel.add(AddEnum);
		panel.add(DelElem);
		panel.add(ModElem);
		panel.add(SetContext);
		panel.add(ValidateElem);
		panel.add(Close);
		
		return panel;
	}
	
	private JScrollPane getCentralArea() 
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1,2));
		if(!updateInputCD.currentInputCD.getClasses().isEmpty())
			panel.add(getClassTable());
		if(!updateInputCD.currentInputCD.getEnums().isEmpty())
			panel.add(getEnumTable());
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.getViewport().add(panel);
		
		return scrollPane;
	}
	
	@SuppressWarnings("serial")
	private JPanel getEnumTable()
	{
		JPanel tabella = new JPanel();
		
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		tabEnum = new JTable(modelloRicerca){public boolean isCellEditable(int rowIndex, int vColIndex){return false;}@SuppressWarnings("unchecked")
			public java.lang.Class getColumnClass(int columnIndex) {return String.class;}};
		modelloRicerca.addColumn("Enumerations");
		tabEnum.setAutoCreateColumnsFromModel(false);								
		tabEnum.getTableHeader().setReorderingAllowed(false);
		tabEnum.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tabEnum.setFillsViewportHeight(true);
		int lines = 1;
		
		int max = 0;
		if(updateInputCD.currentInputCD.getEnums()!=null)
			max=updateInputCD.currentInputCD.getEnums().size();
		for (int i= 0; i<max; i++)
		{
			modelloRicerca.addRow(new Object[]{updateInputCD.currentInputCD.getEnums().get(i)});
			if(auxiliary_I_Team.getLineCount(updateInputCD.currentInputCD.getEnums().get(i),43)>lines)
				lines=auxiliary_I_Team.getLineCount(updateInputCD.currentInputCD.getEnums().get(i),43);
		}
		
		tabEnum.addMouseListener(new MouseAdapter() 
		{
			public void mouseClicked(MouseEvent e)
		    {
		        if (e.getClickCount() == 1)
		        	try
		        	{
		        		tabClass.clearSelection();
	        		}
		        	catch (Exception ArrayIndexOutOfBoundsException) {}
		    }
		});
			  
		tabEnum.setRowHeight(tabEnum.getRowHeight() * lines);
		tabEnum.setDefaultRenderer(String.class, new MultiLineCellRenderer());
		tabella.setLayout(new BorderLayout());
		tabella.add(tabEnum.getTableHeader(), BorderLayout.NORTH);
		tabella.add(tabEnum, BorderLayout.CENTER);
		
		return tabella;
	}
	
	@SuppressWarnings("serial")
	private JPanel getClassTable()
	{
		JPanel tabella = new JPanel();
		
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		tabClass = new JTable(modelloRicerca){public boolean isCellEditable(int rowIndex, int vColIndex){return false;}@SuppressWarnings("unchecked")
			public java.lang.Class getColumnClass(int columnIndex) {return String.class;}};
		modelloRicerca.addColumn("Classes");
		tabClass.setAutoCreateColumnsFromModel(false);								
		tabClass.getTableHeader().setReorderingAllowed(false);
		tabClass.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tabClass.setFillsViewportHeight(true);
		int lines=1;
		
		int max = 0;
		if(updateInputCD.currentInputCD.getClasses()!=null)
			max=updateInputCD.currentInputCD.getClasses().size();
		for (int i= 0; i<max; i++)
		{
			modelloRicerca.addRow(new Object[]{updateInputCD.currentInputCD.getClasses().get(i)});
			if(auxiliary_I_Team.getLineCount(updateInputCD.currentInputCD.getClasses().get(i),43)>lines)
				lines=auxiliary_I_Team.getLineCount(updateInputCD.currentInputCD.getClasses().get(i),43);
		}
		
		tabClass.addMouseListener(new MouseAdapter() 
		{
			public void mouseClicked(MouseEvent e)
		    {
		        if (e.getClickCount() == 1)
		        	try
		        	{
		        		tabEnum.clearSelection();
		        	}
		        	catch (Exception ArrayIndexOutOfBoundsException) {}
		    }
		});
		
		tabClass.setRowHeight(tabClass.getRowHeight() * lines);
		tabClass.setDefaultRenderer(String.class, new MultiLineCellRenderer());
		tabella.setLayout(new BorderLayout());
		tabella.add(tabClass.getTableHeader(), BorderLayout.NORTH);
		tabella.add(tabClass, BorderLayout.CENTER);
		
		return tabella;
	}
	
	private JLabel getNorthLabel() 
	{
		JLabel label = new JLabel("Show Class Diagram");
		label.setFont(new Font("Tahoma", Font.BOLD, 24));
		
		return label;
	}
	
	public void display(JPanel daVisualizzare)
	{
		container.removeAll();
		container.add(daVisualizzare);
		container.setVisible(true);
		container.revalidate();
		
	}
	
	public JPanel showCD()
	{
		container.removeAll();
		container.add(showClassDiagram());
		container.setVisible(true);
		container.revalidate();
		
		return container;
	}
	
	public void showMessage(String string) 
	{
		JOptionPane.showMessageDialog(container, string, "Error", JOptionPane.WARNING_MESSAGE);
	}
}
